package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.commom.exception.CustException;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApCollection;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.behavior.pojo.ApUnlikesBehavior;
import com.heima.model.behavior.vos.AppArticleRelationshipVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author by jojo
 * @Date 2022/3/9
 * @Description
 */
@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 加载文章详情 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
//        1. 判断用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        AppArticleRelationshipVo vo = new AppArticleRelationshipVo(false,false,false,false);
        //        2. 未登录 直接将 4种行为 均返回false
        if (apUser == null) {
            return ResponseResult.okResult(vo);
        } else {//  3. 如果登录
                //  4. 查询行为实体
            Integer userId = apUser.getId();
            Long articleId = dto.getArticleId();
            Integer equipmentId = dto.getEquipmentId();
            Integer authorApUserId = dto.getAuthorApUserId();
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, equipmentId);
            String entryId = apBehaviorEntry.getId();
            //  5. 根据行为实体 文章ID查询 是否点赞
            Query query = Query.query(Criteria.where("entryId").is(entryId).and("articleId").is(articleId));
            ApLikesBehavior likeOne = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (likeOne!=null) {
                vo.setIslike(true);
            }
            //  6. 根据行为实体 文章ID查询 是否不喜欢
            ApUnlikesBehavior unLikeOne = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (unLikeOne!=null) {
                vo.setIsunlike(true);
            }
            //  7. 根据行为实体 文章ID查询 是否收藏
            ApCollection collectionOne = mongoTemplate.findOne(query, ApCollection.class);
            if (collectionOne!=null) {
                vo.setIscollection(true);
            }
            //  8. 更具登录用户id 去redis中查询是否关注该作者
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + userId, String.valueOf(authorApUserId));
            if (score!=null) {
                vo.setIsfollow(true);
            }
            //  9. 封装结果 返回
           return ResponseResult.okResult(vo);
        }
    }
}
